package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import org.piaohao.model.Driver;
import org.piaohao.model.Employee;
import org.piaohao.model.Warn;
import org.piaohao.model.WarnResult;
import org.piaohao.task.DriverAgeTask;
import org.piaohao.task.EmpEduTask;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 17:24:01
 *
 * @author piaohao
 */
public class WarnController extends Controller {

    public static final String moduleName = "预警模块";

    public void setting() {
        render("/pages/warn/setting.jsp");
    }

    public void index() {
        render("/pages/warn/warn.jsp");
    }

    public void list() {
        try {
            Integer start = getParaToInt("start");
            Integer limit = getParaToInt("limit");
            String orgId = (String) getSessionAttr("orgId");
            List<Record> warns = null;
            Long total = 0L;
            if (orgId.equals("0")) {
                warns = Db.find("select w.* ,o.name orgName from t_warn w left join t_organization o on w.org_id=o.id limit ?,?", start, limit);
                total = Db.queryLong("select count(*) from t_warn");
            } else {
                warns = Db.find("select w.* ,o.name orgName from t_warn w left join t_organization o on w.org_id=o.id where o.id=? limit ?,?", orgId, start, limit);
                total = Db.queryLong("select count(*) from t_warn where org_id=?", orgId);
            }
            renderJson(JsonUtil.printGrid(total, warns));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void resultList() {
        try {
            Integer start = getParaToInt("start");
            Integer limit = getParaToInt("limit");
            List<WarnResult> warns = WarnResult.dao.find("select * from t_warn_result limit ?,?", start, limit);
            Long total = Db.queryLong("select count(*) from t_warn_result");
            renderJson(JsonUtil.printGrid(total, warns));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getWarnById() {
        try {
            String id = getPara("warnId");
            Warn warn = Warn.dao.findById(id);
            String[] empIds = warn.getStr("emp_ids").split(",");
            List<Record> emps = new ArrayList<Record>();
            for (int i = 0; i < empIds.length; i++) {
                Record emp = Db.findFirst("select e.* ,o.name orgName from t_employee e left join t_organization o on e.org_id=o.id where e.id=?", empIds[i]);
                emps.add(emp);
            }
            Map map1 = new HashMap();
            Map map2 = new HashMap();
            map1.put("warn", warn);
            map2.put("emps", emps);
            renderJson(JsonUtil.printSuccess(true, "成功", map1, map2));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void add() {
        try {
            Warn warn = getModel(Warn.class);
            warn.set("id", UUIDGenerator.getUUID());
            warn.save();
            if (warn.getStr("status").equals("启用")) {
                if (warn.getInt("type") == 1) {
                    Timer timer = new Timer();
                    timer.schedule(new DriverAgeTask(warn), new Date(), 24 * 60 * 60 * 1000);
                } else if (warn.getInt("type") == 2) {
                    Timer timer = new Timer();
                    timer.schedule(new EmpEduTask(warn), new Date(), 24 * 60 * 60 * 1000);
                }
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Warn warn = getModel(Warn.class);
            warn.update();
            if (warn.getStr("status").equals("启用")) {
                if (warn.getInt("type") == 1) {
                    Timer timer = new Timer();
                    timer.schedule(new DriverAgeTask(warn), new Date(), 24 * 60 * 60 * 1000);
                } else if (warn.getInt("type") == 2) {
                    Timer timer = new Timer();
                    timer.schedule(new EmpEduTask(warn), new Date(), 24 * 60 * 60 * 1000);
                }
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Record warn = Db.findFirst("select w.* ,o.name orgName from t_warn w left join t_organization o on w.org_id=o.id where w.id=?", id);
            String[] empIds = warn.getStr("emp_ids").split(",");
            List<Record> emps = new ArrayList<Record>();
            for (int i = 0; i < empIds.length; i++) {
                Record emp = Db.findFirst("select e.* ,o.name orgName from t_employee e left join t_organization o on e.org_id=o.id where e.id=?", empIds[i]);
                emps.add(emp);
            }
            setAttr("emps", emps);
            setAttr("warn", warn);
            render("/pages/warn/warnDetail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showResultDetail() {
        try {
            List<Driver> drivers = new ArrayList<Driver>();
            List<Employee> emps = new ArrayList<Employee>();
            String id = getPara(0);
            WarnResult wr = WarnResult.dao.findById(id);
            if (wr.getInt("type") == 1) {
                String[] fkIds = wr.getStr("fk_ids").split(",");
                int total = fkIds.length;
                for (int i = 0; i < fkIds.length; i++) {
                    Driver d = Driver.dao.findById(fkIds[i]);
                    d.keep("name", "id_card", "cellphone","age");
                    drivers.add(d);
                }
                wr.remove("fk_ids");
                setAttr("warnResult", wr);
                setAttr("total", total);
                setAttr("drivers", drivers);
                render("/pages/warn/warnResultDetail1.jsp");
            }else if (wr.getInt("type") == 2) {
                String[] fkIds = wr.getStr("fk_ids").split(",");
                int total = fkIds.length;
                for (int i = 0; i < fkIds.length; i++) {
                    Employee e = Employee.dao.findById(fkIds[i]);
                    e.keep("name", "id_card", "phone","education");
                    emps.add(e);
                }
                wr.remove("fk_ids");
                setAttr("warnResult", wr);
                setAttr("total", total);
                setAttr("emps", emps);
                render("/pages/warn/warnResultDetail2.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
